//
//  TodayCell.m
//  Pianke
//
//  Created by BetaTheta on 16/1/20.
//  Copyright © 2016年 BetaTheta. All rights reserved.
//

#import "TodayCell.h"

@interface TodayCell ()
@end
@implementation TodayCell
// 内存管理
- (void)dealloc
{
    [_nameLabel release];
    [_ennameLabel release];
    [_coverimg release];
    [_titleLabel release];
    [_contentLabel release];
    [_likeBtn release];
    [_likeCount release];
    [super dealloc];
}
// 初始化
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        //
        self.nameLabel = [[UILabel alloc] init];
//        _nameLabel.backgroundColor = [UIColor redColor];
        [self.contentView addSubview:_nameLabel];
        [_nameLabel release];
        //
        self.ennameLabel = [[UILabel alloc] init];
//        _ennameLabel.backgroundColor = [UIColor yellowColor];
        [self.contentView addSubview:_ennameLabel];
        [_ennameLabel release];
        //
        self.coverimg = [[UIImageView alloc] init];
        _coverimg.backgroundColor = [UIColor orangeColor];
        [self.contentView addSubview:_coverimg];
        [_coverimg release];
        //
        self.titleLabel = [[UILabel alloc] init];
//        _titleLabel.backgroundColor = [UIColor purpleColor];
        [self.contentView addSubview:_titleLabel];
        [_titleLabel release];
        //
        self.contentLabel = [[UILabel alloc] init];
//        _contentLabel.backgroundColor = [UIColor blueColor];
        [self.contentView addSubview:_contentLabel];
        _contentLabel.numberOfLines = 0;
        _contentLabel.font = [UIFont systemFontOfSize:15];
        [_contentLabel release];
        //
        self.likeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
//        _likeBtn.backgroundColor = [UIColor cyanColor];
        [self.contentView addSubview:_likeBtn];
        [_likeBtn release];
        //
        self.likeCount = [[UILabel alloc] init];
//        _likeCount.backgroundColor = [UIColor magentaColor];
        [self.contentView addSubview:_likeCount];
        [_likeCount release];
        
    }
    return self;
}
// 计算文本高度方法
+ (CGFloat)heightWithText:(NSString *)text
{
    // 保存文本属性(大小)
    NSDictionary *dic = @{NSFontAttributeName:[UIFont systemFontOfSize:15]};
    // 预设尺寸 文本最大不超过这个尺寸
    // 根据设置的宽度计算高度(宽度和label保持一致)
    CGSize size = CGSizeMake(414 - 20, 1000);//?? 414
    // 根据文本 计算出一个CGRect
    // 参数1: 尺寸范围
    // 参数2: 文本属性
    // 参数3: 按照什么方式获取rect
    // 参数4: nil
    CGRect rect = [text boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
    
    // 返回得到的高度
    return rect.size.height;
}

// 布局
- (void)layoutSubviews
{
    _nameLabel.frame = CGRectMake(10, 5, 50, 20);
    _ennameLabel.frame = CGRectMake(60, 5, 50, 20);
    _coverimg.frame = CGRectMake(20, 30, self.contentView.frame.size.width - 40, 150);
    _titleLabel.frame = CGRectMake(20, 180, 200, 40);
    CGFloat textH = [[self class] heightWithText:_contentLabel.text];
    _contentLabel.frame = CGRectMake(20, 230, self.contentView.frame.size.width - 40, textH);
    _likeBtn.frame = CGRectMake(300, 300 + textH, 30, 30);
    _likeCount.frame = CGRectMake(330, 300 + textH, 30, 30);
}


- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
